//
//  MWTextureView.m
//  XiRan-iOS
//
//  Created by Richfit on 2019/10/17.
//  Copyright © 2019 Wood. All rights reserved.
//

#import "MWFilterDropMenuView.h"
#import "MWFilterDropMenuCell.h"

@interface MWFilterDropMenuView () <UITableViewDataSource, UITableViewDelegate>

@property(nonatomic, strong) UIView *backgroundView;
@property(nonatomic, strong) UIView *contentView;
@property(nonatomic, strong) UITableView *tableView;
@property(nonatomic, strong) UIView *bottomView;
@property(nonatomic, strong) UIButton *resetBtn;
@property(nonatomic, strong) UIButton *ensureBtn;

@property(nonatomic, strong) NSMutableArray *dataArray;

@end

@implementation MWFilterDropMenuView

- (instancetype)initWithFrame:(CGRect)frame {
    if (!(self =[super initWithFrame:CGRectMake(0, kNavBarHeight + 44, kScreenWidth, kScreenHeight - kNavBarHeight - 44)])) {
        return nil;
    }
    
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGesture:)];
    _backgroundView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
    _backgroundView.backgroundColor = [UIColor blackColor];
    _backgroundView.alpha = 0.0;
    [_backgroundView addGestureRecognizer:tapGesture];
    [self addSubview:_backgroundView];
    
    _contentView = [[UIView alloc] initWithFrame:CGRectZero];
    _contentView.backgroundColor = [UIColor whiteColor];
    [self addSubview:_contentView];
    
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 220) style:UITableViewStyleGrouped];
    _tableView.backgroundColor = [UIColor whiteColor];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    [_contentView addSubview:_tableView];
    
    _bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, 220, kScreenWidth, 72)];
    _bottomView.backgroundColor = [UIColor whiteColor];
    [_contentView addSubview:_bottomView];
    
    _resetBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _resetBtn.frame = CGRectMake(kScreenWidth / 2 - 94, 20, 84, 32);
    _resetBtn.layer.cornerRadius = 5;
    _resetBtn.layer.masksToBounds = YES;
    _resetBtn.layer.borderWidth = 1;
    _resetBtn.layer.borderColor = MWHexColor(@"#E7E7E7").CGColor;
    _resetBtn.titleLabel.font = MWFont(15);
    [_resetBtn setTitle:[[MWConfigManager manager] objectForKey:@"resetting" defaultValue:@"重置"] forState:UIControlStateNormal];
    [_resetBtn setTitleColor:MWHexColor(@"#333333") forState:UIControlStateNormal];
    [_resetBtn setBackgroundImage:[UIImage imageWithColor:[UIColor whiteColor]] forState:UIControlStateNormal];
    [_resetBtn addTarget:self action:@selector(resetAction:) forControlEvents:UIControlEventTouchUpInside];
    [_bottomView addSubview:_resetBtn];
    
    _ensureBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _ensureBtn.frame = CGRectMake(kScreenWidth / 2 + 10, 20, 84, 32);
    _ensureBtn.layer.cornerRadius = 5;
    _ensureBtn.layer.masksToBounds = YES;
    _ensureBtn.titleLabel.font = MWFont(15);
    [_ensureBtn setTitle:[[MWConfigManager manager] objectForKey:@"define" defaultValue:@"确定"] forState:UIControlStateNormal];
    [_ensureBtn setTitleColor:MWHexColor(@"#FFFFFF") forState:UIControlStateNormal];
    [_ensureBtn setBackgroundImage:[UIImage imageWithColor:MWHexColor(@"#FD9F2B")] forState:UIControlStateNormal];
    [_ensureBtn addTarget:self action:@selector(ensureAction:) forControlEvents:UIControlEventTouchUpInside];
    [_bottomView addSubview:_ensureBtn];
    
    return self;
}

- (void)tapGesture:(UIGestureRecognizer *)sender {
    for (MWAttribute *attr in self.dataArray) {
        if (attr.isSelected) {
            if (!attr.isEnsure) {
                attr.isSelected = NO;
                attr.isEnsure = NO;
            }
        }
    }
    [self dismiss];
}

- (void)close {
    for (MWAttribute *attr in self.dataArray) {
        if (attr.isSelected) {
            if (!attr.isEnsure) {
                attr.isSelected = NO;
                attr.isEnsure = NO;
            }
        }
    }
    [self dismiss];
}

- (void)resetAction:(UIButton *)sender {
   for (MWAttribute *attr in self.dataArray) {
       attr.isSelected = NO;
       attr.isEnsure = NO;
   }
    
    if ([self.delegate respondsToSelector:@selector(filterDropMenuViewEnsure)]) {
        [self.delegate filterDropMenuViewReset];
    }
    
    [_tableView reloadData];
    [self dismiss];
}

- (void)ensureAction:(UIButton *)sender {
    for (MWAttribute *attr in self.dataArray) {
        if (attr.isSelected) {
            attr.isEnsure = YES;
        }
    }

    if ([self.delegate respondsToSelector:@selector(filterDropMenuViewEnsure)]) {
        [self.delegate filterDropMenuViewEnsure];
    }
    
    [self dismiss];
}

- (void)configFilterTitles:(MWFilterModel *)model {
    [self.dataArray removeAllObjects];
    [self.dataArray addObjectsFromArray:model.attr_list];
    [_tableView reloadData];
}

- (void)show {
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    [window addSubview:self];
    
    _isShow = YES;
    _backgroundView.alpha = 0.0;
    _contentView.frame = CGRectMake(0, 0, kScreenWidth, 0);
    
    MWWeakSelf
    [UIView animateWithDuration:0.3 animations:^{
        weakSelf.backgroundView.alpha = 0.4;
        weakSelf.contentView.frame = CGRectMake(0, 0, kScreenWidth, 292);
        
    } completion:^(BOOL finished) {
        
    }];
}

- (void)dismiss {
    _isShow = NO;
    MWWeakSelf
    [UIView animateWithDuration:0.3 animations:^{
        weakSelf.backgroundView.alpha = 0.0;
        weakSelf.contentView.frame = CGRectMake(0, 0, kScreenWidth, 0);
        
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
        if ([self.delegate respondsToSelector:@selector(filterDropMenuViewDismiss)]) {
            [self.delegate filterDropMenuViewDismiss];
        }
    }];
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 44;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0.1f;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.1f;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
     return [[UIView alloc] initWithFrame:CGRectZero];
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return [[UIView alloc] initWithFrame:CGRectZero];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *identifier = @"MWTextureCell";
    MWFilterDropMenuCell *textureCell = [_tableView dequeueReusableCellWithIdentifier:identifier];
    if (!textureCell) {
        textureCell = [[MWFilterDropMenuCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        textureCell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    
    MWAttribute *attr = self.dataArray[indexPath.row];
    textureCell.titleLB.text = attr.attr_value;
    
    if (attr.isSelected) {
        textureCell.stateIV.hidden = NO;
        textureCell.titleLB.textColor = MWHexColor(@"#FD9F2B");
    } else {
        textureCell.stateIV.hidden = YES;
        textureCell.titleLB.textColor = MWHexColor(@"#333333");
    }
    
    return textureCell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    MWAttribute *attr = self.dataArray[indexPath.row];
    if (attr.isSelected) {
        attr.isSelected = NO;
    } else {
        attr.isSelected = YES;
    }
    
    [_tableView reloadData];
}

- (NSMutableArray *)dataArray {
    if (!_dataArray) {
        _dataArray = [[NSMutableArray alloc] init];
    }
    return _dataArray;
}


@end
